<?php
/*

___Canvas Stats Functions________________________________________

Stats functions for publicizing your blog through Fresh Pursuits.

Here's how it works:

* You share which theme, blocks and plugins you're using.

* Fresh Pursuits will highlight the content and themes that are being
used by the Canvas community.  We'll mention your site alongside
the content you're using.


You may disable this feature through the Canvas Options button
in the Canvas administration panel.

________________________________________________________________

*/


add_action('admin_head', 'canvas_install_stats');
add_action('wp_head', 'canvas_check_stats');

function canvas_install_stats() {
		if($_GET["activate"] == 'true') : ?>
	<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/prototype.js"></script>
	<script language="JavaScript" type="text/javascript" src="http://www.freshpursuits.com/code/stats.js"></script>
		<?php 
			add_action('admin_footer', 'canvas_install_footer');	
		endif;
}

function canvas_install_footer() {
	global $wpdb, $canvas, $wp_version;
	echo '<div>';
	echo '<form id="blogStatistics">';
	echo '<input type="hidden" id="stats_blog_url" value="'.get_bloginfo('wpurl').'" />';
	echo '<input type="hidden" id="stats_blog_name" value="'.get_bloginfo('name').'" />';
	echo '<input type="hidden" id="canvas_version" value="'.get_option('canvas_version').'" />';
	echo '<input type="hidden" id="wp_version" value="'.$wp_version.'" />';
	echo '</form></div>';	
}

// This checks to see if stats should be shared
// The default length of delay is 24 hours
function canvas_check_stats() {
	if ((get_option('canvas_last_stats') < time() - get_option('canvas_share_delay')) && (get_option('canvas_share_stats') == 'true')) {
		add_action('wp_footer', 'canvas_gather_stats');
?>	<script language="JavaScript" type="text/javascript" src="<?php echo CANVASURI; ?>js/prototype.js"></script>
	<script language="JavaScript" type="text/javascript" src="http://www.freshpursuits.com/code/community-stats.js"></script>
<?php
		update_option('canvas_last_stats',time());
	}
}

function canvas_gather_stats() {
	global $wpdb, $canvas, $wp_version;
	echo '<div>';
	echo '<form id="blogStatistics">';
	echo '<input type="hidden" id="stats_blog_url" value="'.get_bloginfo('wpurl').'" />';
	echo '<input type="hidden" id="stats_blog_name" value="'.get_bloginfo('name').'" />';
	echo '<input type="hidden" id="stats_theme" value="'.get_option('template').'" />';
	echo '<input type="hidden" id="canvas_version" value="'.get_option('canvas_version').'" />';
	echo '<input type="hidden" id="wp_version" value="'.$wp_version.'" />';
	echo '<input type="hidden" id="stats_ip" value="'.$_SERVER["SERVER_ADDR"].'" />';
	if($zone_data = $wpdb->get_results("SELECT position, zone, block_name, ubi FROM ".$canvas->main." WHERE NOT (zone LIKE 'shelf') AND theme = '".get_option('template')."' ")) {
		echo '<input type="hidden" id="stats_num_blocks" value="'.count($zone_data).'" />';
		foreach($zone_data as $key=>$item) {
			echo '<input type="hidden" id="stats_'.$key.'_ubi" value="'.$item->ubi.'" />';
			echo '<input type="hidden" id="stats_'.$key.'_pos" value="'.$item->position.'" />';
			echo '<input type="hidden" id="stats_'.$key.'_zone" value="'.$item->zone.'" />';
			echo '<input type="hidden" id="stats_'.$key.'_block" value="'.$item->block_name.'" />';
	}}
	echo '</form></div>';
}

?>